package com.itboye.sunsun.person.ui;

import java.util.List;

import com.itboye.sunsun.MainActivity;
import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.LocationBean;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.AddressFragment.GetInforListener;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddLocationActivity extends BaseActivity implements OnClickListener, GetInforListener {

	private RelativeLayout locationrela;
	private RelativeLayout backrela;
	private RelativeLayout saverela;
	private RelativeLayout topbar;
	private TextView provincetext;
	private EditText nameedit;
	private EditText phoneedit;
	private EditText detail;
	private EditText zipcode;
	private String provincename;
	private String cityname;
	private String districtname;
	private String provinceno;
	private String cityno;
	private String districtno;
	private List<LocationBean> list;
	public static String CHANGEADDRESS="com.itboye.person,changeaddress";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		Intent intent=getIntent();
		String from=intent.getStringExtra("from");
		
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addlocation);
		
		if(from.equals("shop"))
		{
		     setStatusBarColor(R.color.orange);
		     topbar.setBackground(getResources().getDrawable(R.color.orange));
		     saverela.setBackground(getResources().getDrawable(R.color.orange));
		}else if(from.equals("person"))
		{
			setStatusBarColor(R.color.top_blue);
		     topbar.setBackground(getResources().getDrawable(R.color.top_blue));
		     saverela.setBackground(getResources().getDrawable(R.color.top_blue));
		}
		getAllLocation();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.locationrela:
			AddressFragment addressFragment = new AddressFragment();
			addressFragment.show(getSupportFragmentManager(), "addressfragment");
			break;
		case R.id.backrela:
			this.finish();
			break;
		case R.id.saverela:
			saveLocation();
			break;
		default:
			break;
		}

	}

	//保存地址
	public void saveLocation() {

		// provincename=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("provincename");
		// cityname=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("cityname");
		// districtname=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("districtname");
		// provinceno=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("provinceno");
		// cityno=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("cityno");
		// districtno=getSupportFragmentManager().findFragmentByTag("123").getArguments().getString("districtno");

		String zipcodeString=zipcode.getText().toString();
		if(zipcodeString.equals(""))
		{
			zipcodeString="无";
		}
		
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_Address_add")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.param("country", "1017")
				.param("provinceid", provinceno)
				.param("cityid", cityno)
				.param("areaid", districtno)
				.param("detailinfo", detail.getText().toString())
				.param("contactname", nameedit.getText().toString())
				.param("mobile", phoneedit.getText().toString())
				.param("province", provincename).param("city", cityname)
				.param("area", districtname)
				.param("postal_code", zipcodeString)
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						Toast.makeText(App.ctx, "添加地址成功", 0).show();
						
						if(list.size()==0)
						{
							setdefaultLocation(response.toString());
						}
						
						
						Intent intent=new Intent(CHANGEADDRESS);
						sendBroadcast(intent);
						
						AddLocationActivity.this.finish();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
							Toast.makeText(App.ctx, "保存地址失败,请稍后重试", 0).show();

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	@Override
	public void onGetinforListener(String province, String city, String district, String provinceNo, String cityNo,
			String districtNo) {
		this.provincename = province;
		this.cityname = city;
		this.districtname = district;
		this.provinceno = provinceNo;
		this.cityno = cityNo;
		this.districtno = districtNo;
		if (city.equals("市辖区")|city.equals("县"))
			city="";
		if(district.equals("市辖区"))
			district="";
		provincetext.setText(province + " " + city + " " + district);

	}
	
	//查询我先在有的地址列表，如果一个都没有的话这个地址设置为默认地址
	public void getAllLocation() {
		MyJsonRequest<List<LocationBean>> request = new MyJsonRequest.Builder<List<LocationBean>>().apiVer("100")
				.typeKey("BY_Address_queryNoPaging")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))

				.requestListener(new XRequestListener<List<LocationBean>>() {

					@Override
					public void onResponse(List<LocationBean> myList) {
						// TODO Auto-generated method stub
						DebugLog.v("result", myList.size() + "");
						list=myList;
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}
	
	// 设置默认地址
		public void setdefaultLocation(String id) {
			MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100")
					.typeKey("BY_Address_setDefaultAddress")
					.param("id", id)
					.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
					.requestListener(new XRequestListener<String>() {

						@SuppressLint("NewApi")
						@Override
						public void onResponse(String response) {

						}
					}).errorListener(new XErrorListener() {

						@Override
						public void onErrorResponse(Exception exception, int code, String msg) {

						}
					}).build();
			HttpRequest.getDefaultRequestQueue().add(request);

		}

}
